class Solution {
public:
    vector<int> sortedSquares(vector<int>& nums) 
    {
        vector<int> ans;
        int j=0;
        while(j<nums.size()&&nums[j]<0)
        {
            j++;
        }
        //
        int endi=0;
        int i=j-1;
        int endj=nums.size()-1;

        while(i>=endi&&j<=endj)
        {
            if((nums[i]*nums[i])<(nums[j]*nums[j]))
            {
                ans.push_back(nums[i]*nums[i]);
                i--;
            }
            else
            {
                ans.push_back(nums[j]*nums[j]);
                j++;
            }
        }
        while(i>=endi)
        {
            ans.push_back(nums[i]*nums[i]);
            i--;
        }
        while(j<=endj)
        {
            ans.push_back(nums[j]*nums[j]);
            j++;
        }
        return ans;

    }
};
